package com.nari.ncps.screen.service.impl.coal;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.coal.CoalOpermonEquipUse;
import com.nari.ncps.screen.mapper.coal.CoalOpermonEquipUseMapper;
import com.nari.ncps.screen.service.coal.CoalOpermonEquipUseService;
import com.nari.ncps.screen.vo.coal.CoalEquipmentUsageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运行检测设备动用信息表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class CoalOpermonEquipUseServiceImpl extends ServiceImpl<CoalOpermonEquipUseMapper, CoalOpermonEquipUse> implements CoalOpermonEquipUseService {

    @Autowired
    private CoalOpermonEquipUseMapper coalOpermonEquipUseMapper;

    @Override
    public List<CoalEquipmentUsageVO> getEquipmentUsage(Integer year) {
//        // 获取当前日期
//        LocalDate currentDate = LocalDate.now();
//
//        // 转换为VO列表
//        List<CoalEquipmentUsageVO> resultList = new ArrayList<>();
//
//        // 计算6个月前的日期
//        LocalDate sixMonthsAgo = currentDate.minusMonths(5);
//
        List<CoalEquipmentUsageVO> resultList = new ArrayList<>();
        LocalDate nowDate = LocalDate.now();
        for (int i = 0; i <6; i++) {
            LocalDate currentDate = nowDate.minusMonths(i);
            int currentDateYear = currentDate.getYear();
            int currentMonthValue = currentDate.getMonthValue();
            LambdaQueryWrapper<CoalOpermonEquipUse> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(CoalOpermonEquipUse::getYear,currentDateYear)
                    .eq(CoalOpermonEquipUse::getMonth,currentMonthValue)
                    .eq(CoalOpermonEquipUse::getOrgCode,"100360")
                    .eq(CoalOpermonEquipUse::getDateType,1);
            List<CoalOpermonEquipUse> list = this.list(wrapper);
            CoalEquipmentUsageVO coalEquipmentUsageVO = new CoalEquipmentUsageVO();
            BigDecimal equipmentCount =  new BigDecimal(0);
            BigDecimal equipActualCount =  new BigDecimal(0);

            // 运输车出动率
            BigDecimal truckUsageRate =  new BigDecimal(0);
            // 附属设备出动率
            BigDecimal auxiliaryUsageRate =  new BigDecimal(0);
            // 电铲出动率
            BigDecimal shovelUsageRate =  new BigDecimal(0);

            // 运输车 设备动用数
            // 运输车 设备出动数
            BigDecimal truckEquipActualCount =  new BigDecimal(0);
            BigDecimal truckEquipUtilizationCount =  new BigDecimal(0);

            // 附属设备 设备动用数
            // 附属设备 出动数
            BigDecimal auxiliaryEquipActualCount =  new BigDecimal(0);
            BigDecimal auxiliaryEquipUtilizationCount =  new BigDecimal(0);

            // 电铲出 设备动用数
            // 电铲出 出动数
            BigDecimal shovelEquipActualCount =  new BigDecimal(0);
            BigDecimal shovelEquipUtilizationCount =  new BigDecimal(0);


            for(CoalOpermonEquipUse data : list){
                Integer equipmentType = data.getEquipmentType();
                // 设备数量
                equipmentCount = equipmentCount.add(BigDecimal.valueOf(data.getEquipmentCount()));
                if (equipmentType == 1){
                    truckEquipActualCount =  truckEquipActualCount.add(BigDecimal.valueOf(data.getEquipActualCount()));
                    truckEquipUtilizationCount = truckEquipUtilizationCount.add(BigDecimal.valueOf(data.getEquipUtilizationCount()));
                }else if (equipmentType == 2){
                    auxiliaryEquipActualCount =  auxiliaryEquipActualCount.add(BigDecimal.valueOf(data.getEquipActualCount()));
                    auxiliaryEquipUtilizationCount = auxiliaryEquipUtilizationCount.add(BigDecimal.valueOf(data.getEquipUtilizationCount()));
                }else{
                    shovelEquipActualCount =  shovelEquipActualCount.add(BigDecimal.valueOf(data.getEquipActualCount()));
                    shovelEquipUtilizationCount = shovelEquipUtilizationCount.add(BigDecimal.valueOf(data.getEquipUtilizationCount()));
                }

            }

            BigDecimal temp = new BigDecimal(100);

            // 设备出动率=设备出动数/设备动用数× 100%
            truckUsageRate  = truckEquipActualCount.divide(truckEquipUtilizationCount, 2, RoundingMode.HALF_UP).multiply(temp);
            auxiliaryUsageRate  = auxiliaryEquipUtilizationCount.divide(auxiliaryEquipActualCount, 2, RoundingMode.HALF_UP).multiply(temp);
            shovelUsageRate  = shovelEquipUtilizationCount.divide(shovelEquipActualCount, 2, RoundingMode.HALF_UP).multiply(temp);


            coalEquipmentUsageVO.setTruckUsageRate(truckUsageRate);
            coalEquipmentUsageVO.setAuxiliaryUsageRate(auxiliaryUsageRate);
            coalEquipmentUsageVO.setShovelUsageRate(shovelUsageRate);
            coalEquipmentUsageVO.setEquipmentCount(equipmentCount);
            coalEquipmentUsageVO.setYear(currentDateYear);
            coalEquipmentUsageVO.setMonth(currentMonthValue);

            resultList.add(coalEquipmentUsageVO);
        }




        return resultList;
        
//        // 查询最近6个月的数据
//        LambdaQueryWrapper<CoalOpermonEquipUse> wrapper = Wrappers.lambdaQuery();
////        LambdaQueryWrapper<CoalOpermonEquipUse> wrapper = new LambdaQueryWrapper<>();
////
////        List<CoalOpermonEquipUse> list = this.list(wrapper);
////        resultList = BeanUtil.copyToList(list, CoalEquipmentUsageVO.class);
////        return resultList;
//
//        wrapper.ge(CoalOpermonEquipUse::getYear, sixMonthsAgo.getYear())
//                .eq(CoalOpermonEquipUse::getDateType, 1)
//               .orderByDesc(CoalOpermonEquipUse::getYear)
//               .orderByDesc(CoalOpermonEquipUse::getMonth);
//
//        List<CoalOpermonEquipUse> equipmentList = coalOpermonEquipUseMapper.selectList( wrapper);
//        List<CoalEquipmentUsageVO> voList = new ArrayList<>();
//        for(CoalOpermonEquipUse equipment : equipmentList) {
//
//            CoalEquipmentUsageVO vo = new CoalEquipmentUsageVO();
//            vo.setYear(equipment.getYear());
//            vo.setMonth(equipment.getMonth());
//            vo.setEquipmentCount(equipment.getEquipmentCount());
//            vo.setTruckUsageRate(equipment.getTruckRate());
//            vo.setAuxiliaryUsageRate(equipment.getAttechRate());
//            vo.setShovelUsageRate(equipment.getShovelRate());
//            voList.add(vo);
//        }
//
//        return voList;

//        // 按年月分组统计
//        Map<String, MonthlyEquipmentStats> monthlyStatsMap = new HashMap<>();
//
//        for (CoalOpermonEquipUse equipment : equipmentList) {
//            String yearMonth = equipment.getYear() + "-" + equipment.getMonth();
//            MonthlyEquipmentStats stats = monthlyStatsMap.computeIfAbsent(
//                yearMonth,
//                k -> new MonthlyEquipmentStats(equipment.getYear(), equipment.getMonth())
//            );
//
//            // 按设备类型统计
//            int type = equipment.getEquipmentType() != null ? equipment.getEquipmentType() : 0;
//            int count = equipment.getEquipmentCount() != null ? equipment.getEquipmentCount() : 0;
//            int isUsing = equipment.getIsUsing() != null ? equipment.getIsUsing() : 0;
//
//            // 累计设备总数
//            stats.totalCount += count;
//
//            // 按设备类型统计
//            if (type == 0) {
//                // 运输车
//                stats.truckTotal += count;
//                if (isUsing == 1) {
//                    stats.truckUsing += count;
//                }
//            } else if (type == 1) {
//                // 附属设备
//                stats.auxiliaryTotal += count;
//                if (isUsing == 1) {
//                    stats.auxiliaryUsing += count;
//                }
//            } else if (type == 2) {
//                // 电铲
//                stats.shovelTotal += count;
//                if (isUsing == 1) {
//                    stats.shovelUsing += count;
//                }
//            }
//        }
//
//
//        // 转换为VO列表
//        List<CoalEquipmentUsageVO> resultList = new ArrayList<>();
        
//        // 生成最近6个月的数据（按照月份从小到大的顺序）
//        for (int i = 5; i >= 0; i--) {
//            LocalDate monthDate = currentDate.minusMonths(i);
//            String yearMonth = monthDate.getYear() + "-" + monthDate.getMonthValue();
//
//            CoalEquipmentUsageVO vo = new CoalEquipmentUsageVO();
//            vo.setYear(monthDate.getYear());
//            vo.setMonth(monthDate.getMonthValue());
//
//
//            vo.setEquipmentCount(0);
//            vo.setTruckUsageRate(BigDecimal.ZERO);
//            vo.setAuxiliaryUsageRate(BigDecimal.ZERO);
//            vo.setShovelUsageRate(BigDecimal.ZERO);
//
//
//
////            MonthlyEquipmentStats stats = monthlyStatsMap.get(yearMonth);
////            if (stats != null) {
////                vo.setEquipmentCount(stats.totalCount);
////
////                // 计算运输车出动率
////                if (stats.truckTotal > 0) {
////                    BigDecimal truckRate = new BigDecimal(stats.truckUsing)
////                            .divide(new BigDecimal(stats.truckTotal), 4, RoundingMode.HALF_UP)
////                            .multiply(new BigDecimal("100"))
////                            .setScale(2, RoundingMode.HALF_UP);
////                    vo.setTruckUsageRate(truckRate);
////                } else {
////                    vo.setTruckUsageRate(BigDecimal.ZERO);
////                }
////
////                // 计算附属设备出动率
////                if (stats.auxiliaryTotal > 0) {
////                    BigDecimal auxiliaryRate = new BigDecimal(stats.auxiliaryUsing)
////                            .divide(new BigDecimal(stats.auxiliaryTotal), 4, RoundingMode.HALF_UP)
////                            .multiply(new BigDecimal("100"))
////                            .setScale(2, RoundingMode.HALF_UP);
////                    vo.setAuxiliaryUsageRate(auxiliaryRate);
////                } else {
////                    vo.setAuxiliaryUsageRate(BigDecimal.ZERO);
////                }
////
////                // 计算电铲出动率
////                if (stats.shovelTotal > 0) {
////                    BigDecimal shovelRate = new BigDecimal(stats.shovelUsing)
////                            .divide(new BigDecimal(stats.shovelTotal), 4, RoundingMode.HALF_UP)
////                            .multiply(new BigDecimal("100"))
////                            .setScale(2, RoundingMode.HALF_UP);
////                    vo.setShovelUsageRate(shovelRate);
////                } else {
////                    vo.setShovelUsageRate(BigDecimal.ZERO);
////                }
////            } else {
////                vo.setEquipmentCount(0);
////                vo.setTruckUsageRate(BigDecimal.ZERO);
////                vo.setAuxiliaryUsageRate(BigDecimal.ZERO);
////                vo.setShovelUsageRate(BigDecimal.ZERO);
////            }
//
//            resultList.add(vo);
//        }
//
//        return resultList;
    }
    
    /**
     * 月度设备统计数据内部类
     */
    private static class MonthlyEquipmentStats {
        int totalCount = 0;
        int truckTotal = 0;
        int truckUsing = 0;
        int auxiliaryTotal = 0;
        int auxiliaryUsing = 0;
        int shovelTotal = 0;
        int shovelUsing = 0;
        
        public MonthlyEquipmentStats(int year, int month) {
            // 构造函数接收参数但不需要存储
        }
    }
}
